package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.common.R;
import com.erp.entity.Payable;
import com.erp.service.PayableService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 应付账
 */
@Slf4j
@RestController
@RequestMapping("/payable")
@RequiresRoles(value = {"FinancialAdministrator","SupperAdministrator"},logical = Logical.OR)
public class PayableController {

    @Autowired
    private PayableService payableService;

    /**
     * 保存应付账信息
     * @param payable
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Payable payable) {
        log.info("保存应付账信息");

        return payableService.savePayableAndLedger(payable);
    }

    /**
     * 删除应付账信息
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam Long id) {
        log.info("删除应付账信息");

        return payableService.deleteByIdAndUpdateLedger(id);
    }

    /**
     * 更新应付账信息
     * @param payable
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Payable payable){
        log.info("更新应付账信息");

        return payableService.updateByIdAndUpdateLedger(payable);
    }

    /**
     * 根据id查询应付账信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Payable> findById(@PathVariable Long id){
        log.info("根据id查询应付账信息");

        return R.success(payableService.getById(id));
    }

    /**
     * 应付账分页查询
     * @param page 页码
     * @param pageSize 每页条数
     * @return 分页信息
     */
    @GetMapping("/page")
    public R<Page<Payable>> page(int page,int pageSize,String name){
        log.info("应付账分页查询");

        Page<Payable> payablePage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Payable> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (name!=null){
            lambdaQueryWrapper.like(Payable::getInformation,name);
        }
        //根据创建时间升序查询
        lambdaQueryWrapper.orderByAsc(Payable::getCreateDateTime);
        payableService.page(payablePage,lambdaQueryWrapper);

        return R.success(payablePage);
    }

}
